<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'医生列表'),
	array('op'=>'edit','title'=>'添加医生'),	
);
$labels=pdo_getall('jy_zhongyi_label',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['labels']=array();
foreach($labels as $lv){
	$return['labels'][$lv['id']]=$lv['title'];
}
$return['stores']=pdo_getall('jy_zhongyi_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['lessons']=pdo_getall('jy_zhongyi_lesson',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['area_id'])){
		$params['on_areas like']='%'.trim($_RQ['area_id']).'%';
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['lesson_id'])){
		$params['lesson_id']=intval($_RQ['lesson_id']);
		$return['params']['lesson_id']=$_RQ['lesson_id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','name','username','storeid','lesson_id','label','status','show','mid','headimg','on_areas','years','age','createtime','classid');
	$list=pdo_getslice('jy_zhongyi_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['store']=pdo_get('jy_zhongyi_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']));
			$on_areas=str_array($item['on_areas']);
			$item['on_areas']='';
			foreach($on_areas as $ov){
				$areatitle=pdo_getcolumn('jy_zhongyi_area',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ov),'title');
				$item['on_areas'] .=$areatitle.',';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【<red>温馨提示</red>】：添加医生后，务必设置《排班》，这样前端手机才能预约挂号。排班时，最好选择挂诊医院，这样才更好的体验显示！','edit'=>true,'list'=>array(
			array('type'=>'img_string','title'=>'医生','img'=>'headimg',
			'data'=>array(
				array('title'=>'医生名称：','name'=>'name'),
				array('title'=>'用户名：','name'=>'username')
				)
			),
			array('name'=>'storeid','title'=>'所属医院','type'=>'select','options'=>'stores'),
			array('name'=>'lesson_id','width'=>10,'title'=>'所属科室','type'=>'select','options'=>'lessons'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('action'=>'/lesson_pai/','title'=>'排班','type'=>'href'),
		)),
		'search'=>array(
			array('name'=>'lesson_id','width'=>10,'title'=>'所属科室','type'=>'select','options'=>'lessons'),
			array('name'=>'name','width'=>10,'title'=>'医生名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_zhongyi_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['years']=date('Y-m-d',$item['years']);
		unset($item['password']);
	}else{
		$item=array();
	}
	$item['paibans']=json_decode($item['paibans'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回医生列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'headimg','title'=>'头像','type'=>'image'),
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'username','title'=>'用户名','type'=>'string'),
					array('name'=>'password','title'=>'密码','type'=>'password'),
					array('name'=>'mobile','title'=>'手机号','type'=>'string'),
					array('name'=>'tel','title'=>'固定电话','type'=>'string'),
					array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
					array('name'=>'years','title'=>'出生年月','type'=>'time'),
					array('name'=>'storeid','title'=>'所属医院','type'=>'select','options'=>'stores'),
					array('name'=>'lesson_id','title'=>'所属科室','type'=>'select','options'=>'lessons'),
					array('name'=>'price','title'=>'门诊价格','type'=>'string'),
					array('name'=>'lineprice','title'=>'在线咨询价格','type'=>'unitstring','unit'=>'次'),
					array('type'=>'checkbox','title'=>'标签','name'=>'label','help'=>'','options'=>'labels'),					
					array('type'=>'checkbox','title'=>'上班时间','name'=>'on_days','help'=>'','options'=>'on_days'),
					array('name'=>'address','title'=>'现居住地','type'=>'string'),
					array('name'=>'remark','title'=>'医生简介','type'=>'text'),
					array('name'=>'desc','title'=>'医生详情','type'=>'richtext'),
					array('type'=>'choose','title'=>'绑定会员','name'=>'mid','action'=>'get_member','choose_title'=>'选择会员'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
			/*array(
					'title'=>'排班情况',
					'list'=>array(
						array('type'=>'checkbox','title'=>'上班时间','name'=>'on_days','help'=>'','options'=>'on_days'),
						array('title'=>'下面排班情况，如果不设置，就表示默认门诊店，默认时间段9：00~17：00','type'=>'tip'),
						array(
								'type'=>'array',
								'title'=>'排班情况',
								'name'=>'paibans',
								'add_tip'=>'添加一个排班',
									'list'=>array(
										array('type'=>'radio','name'=>'weekid','title'=>'星期','options'=>'on_days'),
										array('type'=>'time_hour','name'=>'ontimes','title'=>'上班时间段'),
										array('type'=>'select','options'=>'stores','name'=>'storeid','title'=>'挂诊医院'),
									),
									'details'=>array(
										array('type'=>'radio','name'=>'weekid','title'=>'星期','options'=>'on_days'),
										array('type'=>'time_hour','name'=>'ontimes','title'=>'上班时间段'),
										array('type'=>'select','options'=>'stores','name'=>'storeid','title'=>'挂诊医院'),
									)
								),
					)
			),*/
		)
	);
	$return['on_days']=array('0'=>'周日','1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六');
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['lesson_id']){
		exi('请选择科室','error');
	}
	//$postdata['time']=ijson_encode($postdata['time']);	
	$data=array_fields(array('lineprice','price','label','paibans','storeid','address','sex','years','name','username','password','mobile','tel','lessondesc','desc','on_num','on_days','headimg','show','remark','paixu','status','lesson_id','mid'),$postdata);
	$data['years']=strtotime($data['years']);
	$data['paibans']=ijson_encode($data['paibans']);
	$id=intval($postdata['id']);
	if($data['password']){
		$data['password']=base64_encode($data['password']);
	}
	if($data['username']){
		$uname=pdo_get('jy_zhongyi_worker',array('username'=>$data['username'],'uniacid'=>$_SESSION['uniacid']));
	}
	if($uname && $uname['id']!=$id){
		exi('账号不可以重复','error');	
	}
	if($id){
		pdo_update('jy_zhongyi_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		if(!$postdata['username'] || !$postdata['password']){
			exi('请填写账户和密码！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_zhongyi_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['area_id'])){
		$params['on_areas like']='%'.trim($_RQ['area_id']).'%';
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['lesson_id'])){
		$params['lesson_id']=intval($_RQ['lesson_id']);
		$return['params']['lesson_id']=$_RQ['lesson_id'];
	}
	$lesson=pdo_getall('jy_zhongyi_worker',$params);
	foreach($lesson as &$item){

	}
	$header=array(
		'id'=>'教师ID',
		'name'=>'教师名称',
		'mobile'=>'联系人电话',
		'username'=>'账号',
	);
	cfc('tools')->export2excel($header,$lesson,'教师表');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_zhongyi_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的医生！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_zhongyi_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}